package com.lgy.springboot_example.utils;

import org.springframework.util.DigestUtils;
import sun.misc.BASE64Encoder;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import java.util.UUID;

public class MyUtils {

    /**
     * MD5加密
     *
     * @param data
     * @return
     */
    public static String parseToMd5(String data) {
        return DigestUtils.md5DigestAsHex(data.getBytes());
    }

    /**
     * 生成唯一token
     *
     * @return
     */
    public static String getToken() {
        String token = (System.currentTimeMillis() + new Random().nextInt(999999999)) + "";
        try {
            MessageDigest md = MessageDigest.getInstance("md5");
            byte[] md5 = md.digest(token.getBytes());
            BASE64Encoder encoder = new BASE64Encoder();
            return encoder.encode(md5);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    /*
     *@Description TODO 生成uuid
     * @author lgy
     * @date 2020/5/12
     * @param []
     * @return java.lang.String
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }
}
